package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.limao.DBDataClass.UserNumDataClass;
import com.limao.DbConnection.DbConnectionManager;

public class UserNumProvider {

	private static class DB_usernum_ProviderContainer {
		private static UserNumProvider instance = new UserNumProvider();
	}

	public static UserNumProvider getInstance() {
		return DB_usernum_ProviderContainer.instance;
	}

	// 定义系统表的name与操作语句
	// wawa_usernum 注册用户数量
	private static final String wawa_usernum_LOAD_DB = "SELECT user_num FROM cd_sys_usernum";
	private static final String wawa_usernum_UPDATE_DB = "UPDATE cd_sys_usernum SET user_num=? where user_num<?";
	private static final String wawa_usernum_INSERT = "INSERT INTO cd_sys_usernum (user_num) VALUES (?)";

	// 初始化
	private UserNumProvider() {

	}

	// 加载某个系统表数据
	public UserNumDataClass Load_System_DB_All() {
		UserNumDataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(wawa_usernum_LOAD_DB); // load
			rs = pstmt.executeQuery();

			while (rs.next()) {
				tmp = new UserNumDataClass();
				tmp.user_num = rs.getInt(1);// 记录id
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}

	// 新建一行记录
	public UserNumDataClass Create_System_DB_All() {
		UserNumDataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(wawa_usernum_INSERT); // load
			pstmt.setInt(1, 0);
			pstmt.execute();

			tmp = new UserNumDataClass();
			tmp.user_num = 0;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}

	// 修改表数据
	public void Updata_System_DB_All(int m) {

		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(wawa_usernum_UPDATE_DB); // load
			pstmt.setInt(1, m);
			pstmt.setInt(2, m);
			pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}

	}

}
